package upb.webserver.test.param;

import com.upb.webserver.common.enums.ChainTypeEnum;
import com.upb.webserver.common.pojo.chain.ReqAddRunningFiscoChainVO;
import com.upb.webserver.common.pojo.order.ReqAddWorkOrderOfNewChainVO;
import com.upb.webserver.common.pojo.order.ReqUpdateWorkOrderOfNewChainVO;
import com.upb.webserver.common.pojo.rest.fabric.data.req.ReqCreateChainDTO;
import com.upb.webserver.common.tools.JsonUtils;
import org.apache.commons.collections4.SetUtils;

import java.util.Arrays;
import java.util.List;

public class ChainParamUtils {

    //--------------------------
    //-------fabric chain-------
    //--------------------------

    /**
     * ReqAddWorkOrderOfNewChainVO
     *
     * @return example: {"name":"��ͬ��","chainType":1,"version":"1.4.4","consensusType":"solo","storageType":"leveldb","description":"desc","fabricOrderOrgList":[{"orgId":1,"companyId":1,"orderNodeList":[{"hostId":1,"nodeName":"orderer1"}]}],"fabricPeerOrgList":[{"orgId":1,"companyId":1,"caNode":{"hostId":1,"nodeName":"ca"},"caPassWd":"adminpw","caUserName":"admin","initUserCount":2,"peerNodeList":[{"hostId":1,"nodeName":"peer1"}]}],"fiscoOrgList":null}
     */
    public static ReqAddWorkOrderOfNewChainVO initReqAddWorkOrderOfNewFabricChainVO() {

        //初始化kafkaBroker
        ReqCreateChainDTO.KafkaBroker kafkaBroker = new ReqCreateChainDTO.KafkaBroker();
        kafkaBroker.setHostName("kafka.example.com");
        kafkaBroker.setIp("127.0.0.1");
        kafkaBroker.setPort(9092);

        //初始化order节点
        ReqAddWorkOrderOfNewChainVO.FabricNode fabricOrderNode = new ReqAddWorkOrderOfNewChainVO.FabricNode();
        fabricOrderNode.setHostId(39);    //order机器id
        fabricOrderNode.setNodeName("orderer");//order节点名称

        //初始化order机构
        ReqAddWorkOrderOfNewChainVO.FabricOrderOrg orderOrg = new ReqAddWorkOrderOfNewChainVO.FabricOrderOrg();
        orderOrg.setOrgId(29);//order机构id
        orderOrg.setCompanyId(7);//order机构所属公司
        orderOrg.setOrderNodeList(Arrays.asList(fabricOrderNode));//order节点列表


        //初始化ca
        ReqAddWorkOrderOfNewChainVO.FabricNode fabricCaNode = new ReqAddWorkOrderOfNewChainVO.FabricNode();
        fabricCaNode.setHostId(27);//ca服务机器id
        fabricCaNode.setNodeName("ca");//ca名称

        //初始化peer节点
        ReqAddWorkOrderOfNewChainVO.FabricNode fabricPeerNode = new ReqAddWorkOrderOfNewChainVO.FabricNode();
        fabricPeerNode.setHostId(27);//peer节点机器id
        fabricPeerNode.setNodeName("peer");//peer节点名称

        //初始化peer机构
        ReqAddWorkOrderOfNewChainVO.FabricPeerOrg peerOrg = new ReqAddWorkOrderOfNewChainVO.FabricPeerOrg();
        peerOrg.setOrgId(16);//peer机构id
        peerOrg.setCompanyId(7);//机构所属公司id
        peerOrg.setCaNode(fabricCaNode);//上面初始化的ca节点
        peerOrg.setCaUserName("admin");//ca用户
        peerOrg.setCaPassWd("adminpw");//ca密码
        peerOrg.setInitUserCount(2);//ca初始化的用户个数
        peerOrg.setPeerNodeList(Arrays.asList(fabricPeerNode));//上面初始化的节点列表

        //初始化工单入参
        ReqAddWorkOrderOfNewChainVO chainParam = new ReqAddWorkOrderOfNewChainVO();
        chainParam.setName("butulu");//链名称
        chainParam.setChainType(ChainTypeEnum.FABRIC.getId());//链类型
        chainParam.setVersion("1.4.4");//底层链版本
        chainParam.setConsensusType("kafka");//共识类型
        chainParam.setKafkaBrokers(Arrays.asList(kafkaBroker));
        chainParam.setStorageType("leveldb");//节点存储类型
        chainParam.setDescription("desc");//描述
        chainParam.setFabricOrderOrgList(Arrays.asList(orderOrg));
        chainParam.setFabricPeerOrgList(Arrays.asList(peerOrg));
        return chainParam;
    }


    /**
     * @return example: {"encryptionId":0,"chainName":"fiscoChain","chainVersion":"v1","description":null,"orgIdSet":[1,3],"frontInfoVOList":[{"orgId":1,"hostId":1,"frontPort":44,"description":"dd"}]}
     */
    public static ReqAddRunningFiscoChainVO initReqAddRunningFiscoChainVO() {
        ReqAddRunningFiscoChainVO.FrontInfoVO frontParam = new ReqAddRunningFiscoChainVO.FrontInfoVO();
        frontParam.setOrgId(1);
        frontParam.setHostId(1);
        frontParam.setFrontPort(44);
        frontParam.setDescription("dd");

        ReqAddRunningFiscoChainVO param = new ReqAddRunningFiscoChainVO();
        param.setChainName("fiscoChain");
        param.setEncryptionId((byte)0);
        param.setChainVersion("v1");
        param.setOrgIdSet(SetUtils.hashSet(1, 3));
        param.setFrontInfoVOList(Arrays.asList(frontParam));
        return param;
    }


    /**
     * @return
     */
    public static ReqAddWorkOrderOfNewChainVO initReqAddWorkOrderOfNewFiscoChainVO() {

        ReqAddWorkOrderOfNewChainVO.NewFiscoChainHost host = new ReqAddWorkOrderOfNewChainVO.NewFiscoChainHost();
        host.setFiscoHostId(1);
        host.setNodeCount(3);


        ReqAddWorkOrderOfNewChainVO.NewFiscoChainOrg org1 = new ReqAddWorkOrderOfNewChainVO.NewFiscoChainOrg();
        org1.setOrgId(1);
        org1.setFiscoHostList(Arrays.asList(host));

        ReqAddWorkOrderOfNewChainVO chain = new ReqAddWorkOrderOfNewChainVO();
        chain.setName("不同链");
        chain.setChainType(ChainTypeEnum.FISCO.getId());
        chain.setVersion("1.4.4");
        chain.setConsensusType("solo");
        chain.setStorageType("leveldb");
        chain.setDescription("desc");
        chain.setFiscoOrgList(Arrays.asList(org1));

        return chain;
    }


    /**
     * @return example:{"workOrderId":12,"caPassWd":"adminpw","caUserName":"admin","initUserCount":2,"caNode":{"hostId":1,"nodeName":"ca"},"peerNodeList":[{"hostId":1,"nodeName":"peer1"}],"fiscoHostList":null}
     */
    public static ReqUpdateWorkOrderOfNewChainVO initReqUpdateWorkOrderOfNewFabricChainVO() {

        //初始化ca
        ReqAddWorkOrderOfNewChainVO.FabricNode fabricCaNode = new ReqAddWorkOrderOfNewChainVO.FabricNode();
        fabricCaNode.setHostId(1);//ca服务机器id
        fabricCaNode.setNodeName("ca");//ca名称

        //初始化peer节点
        ReqAddWorkOrderOfNewChainVO.FabricNode fabricPeerNode = new ReqAddWorkOrderOfNewChainVO.FabricNode();
        fabricPeerNode.setHostId(1);//peer节点机器id
        fabricPeerNode.setNodeName("peer1");//peer节点名称

        //初始化peer机构
        ReqAddWorkOrderOfNewChainVO.FabricPeerOrg peerOrg = new ReqAddWorkOrderOfNewChainVO.FabricPeerOrg();
        peerOrg.setOrgId(1);//peer机构id
        peerOrg.setCompanyId(1);//机构所属公司id
        peerOrg.setCaNode(fabricCaNode);//上面初始化的ca节点
        peerOrg.setCaUserName("admin");//ca用户
        peerOrg.setCaPassWd("adminpw");//ca密码
        peerOrg.setInitUserCount(2);//ca初始化的用户个数
        peerOrg.setPeerNodeList(Arrays.asList(fabricPeerNode));//上面初始化的节点列表




        ReqUpdateWorkOrderOfNewChainVO param = new ReqUpdateWorkOrderOfNewChainVO();
        param.setWorkOrderId(12);
        param.setFabricPeerOrgList(Arrays.asList(peerOrg));
        return param;
    }


    /**
     * @return
     */
    public static ReqUpdateWorkOrderOfNewChainVO initReqUpdateWorkOrderOfNewFiscoChainVO() {
        ReqAddWorkOrderOfNewChainVO.NewFiscoChainHost host = new ReqAddWorkOrderOfNewChainVO.NewFiscoChainHost();
        host.setFiscoHostId(6);
        host.setNodeCount(6);

        ReqAddWorkOrderOfNewChainVO.NewFiscoChainOrg org1 = new ReqAddWorkOrderOfNewChainVO.NewFiscoChainOrg();
        org1.setOrgId(1);
        org1.setFiscoHostList(Arrays.asList(host));

        ReqUpdateWorkOrderOfNewChainVO param = new ReqUpdateWorkOrderOfNewChainVO();
        param.setWorkOrderId(13);
        param.setFiscoOrgList(Arrays.asList(org1));

        return param;
    }


    public static void main(String args[]) {
        String param = JsonUtils.objToString(initReqAddRunningFiscoChainVO());
        System.out.println(param);
    }
}
